package com.yb.web.global.servlet;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;

import com.opensymphony.xwork2.ActionContext;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.bussiness.goods.GoodsService;
import com.yb.bussiness.goods.GoodsServiceImpl;
import com.yb.bussiness.information.InformationService;
import com.yb.bussiness.information.InformationServiceImpl;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.bussiness.statement.StatementService;
import com.yb.bussiness.statement.StatementServiceImpl;
import com.yb.domain.VO.PingouVO;
import com.yb.domain.goods.TgBrand;
import com.yb.domain.goods.TgCategory;
import com.yb.domain.goods.TgGoods;
import com.yb.domain.goods.TgInformation;
import com.yb.domain.statement.Statement;
import com.yb.util.framework.MemoryData;

public class TaskUtil extends TimerTask {
	private static CategoryService categoryService;
	private static BrandService brandService;
	private static InformationService informationService;
	private static GoodsService goodService;
	private static StatementService statementService;
	static {
		categoryService = new CategoryServiceImpl();
		brandService = new BrandServiceImpl();
		informationService = new InformationServiceImpl();
		goodService = new GoodsServiceImpl();
		statementService = new StatementServiceImpl();
	}

	public void run() {
		List<TgCategory> categoryList = findAllCategoryFirstLevel();
		MemoryData.setCategoryList(categoryList);
		Map<Long, List<TgBrand>> brandMap = findHotPingou(categoryList);
		MemoryData.setBrandMap(brandMap);
		Map<Long, List<PingouVO>> pingouMap = findPingouList(categoryList);
		MemoryData.setPingouMap(pingouMap);
		Map<Long, List<TgInformation>> informationMap = findInformationBycategoryId(categoryList);
		MemoryData.setInformationMap(informationMap);
		List<TgInformation> hotInfoMap = findInformationHotBycategoryId();
		MemoryData.setHotInforMap(hotInfoMap);
		List<TgInformation> imgInfoHotList = findImgInformationHot();
		MemoryData.setImgInfoHotList(imgInfoHotList);
		List<TgInformation> imgInfoList = findImgInformation();
		MemoryData.setImgInfoList(imgInfoList);
		Map<Long, List<TgGoods>> hotGoodMap = findGoodHot(categoryList);
		MemoryData.setHotGoodMap(hotGoodMap);
		// Integer totalCount = brandService.findTotalCount();
		Integer totalCount = bulidTotalCount(categoryList);
		MemoryData.setTotalCount(totalCount);
		List<TgInformation> hotActivityList = findHotActivity();
		MemoryData.setHotActivityList(hotActivityList);

		MemoryData.setHotTrendList(bulidHotTrend());
		MemoryData
				.setHotTrendCategoryMap(findHotTrendCategoryMap(categoryList));
		  List<Statement> statementList = findAllStatement();
		MemoryData.setStatementList(statementList);
		TgInformation indexImg = findindexImg();
		MemoryData.setIndexImg(indexImg);
		System.gc();
	}

	private Integer bulidTotalCount(List<TgCategory> categoryList) {
		Integer count = 0;
		for (Iterator iterator = categoryList.iterator(); iterator.hasNext();) {
			TgCategory category = (TgCategory) iterator.next();
			count += category.getCount();
		}
		return count;
	}

	
	
	private List<TgCategory> findAllCategoryFirstLevel() {
		return categoryService.findAll(-1, -1);
	}

	private List<Statement> findAllStatement() {
        return (List<Statement>)statementService.findAll("1", "50").get("list");
    }
	
   private TgInformation findindexImg() {
       return informationService.readBycategoryFlag(9);
	    }
	
	@SuppressWarnings("unchecked")
	private Map<Long, List<TgBrand>> findHotPingou(List<TgCategory> categoryList) {
		Map<Long, List<TgBrand>> map = new HashMap<Long, List<TgBrand>>();
		for (Iterator iterator = categoryList.iterator(); iterator.hasNext();) {
			TgCategory category = (TgCategory) iterator.next();
			map.put(category.getId(), brandService.findHotBrandByCategory(
					category.getId(), 0, 8));
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	private Map<Long, List<PingouVO>> findPingouList(
			List<TgCategory> categoryList) {
		Map<Long, List<PingouVO>> map = new HashMap<Long, List<PingouVO>>();
		for (Iterator iterator = categoryList.iterator(); iterator.hasNext();) {
			TgCategory category = (TgCategory) iterator.next();
			map.put(category.getId(), brandService.findPingouList(category
					.getId(), 0, 10));
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	private Map<Long, List<TgInformation>> findInformationBycategoryId(
			List<TgCategory> categoryList) {
		Map<Long, List<TgInformation>> map = new HashMap<Long, List<TgInformation>>();
		for (Iterator iterator = categoryList.iterator(); iterator.hasNext();) {
			TgCategory category = (TgCategory) iterator.next();
			map.put(category.getId(), informationService.findByCategoryTask(
					category.getId(), 0, 16));
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	private List<TgInformation> findInformationHotBycategoryId() {
		return informationService.findHotInfomation();
	}

	@SuppressWarnings("unchecked")
	private List<TgInformation> findImgInformationHot() {
		List<TgInformation> imgInfoList = informationService.findHotImgInfo();
		return imgInfoList;
	}

	@SuppressWarnings("unchecked")
	private List<TgInformation> findImgInformation() {
		return informationService.findImgInfo();
	}

	@SuppressWarnings("unchecked")
	private Map<Long, List<TgGoods>> findGoodHot(List<TgCategory> categoryList) {
		Map<Long, List<TgGoods>> map = new HashMap<Long, List<TgGoods>>();
		for (Iterator iterator = categoryList.iterator(); iterator.hasNext();) {
			TgCategory category = (TgCategory) iterator.next();
			map.put(category.getId(), (List<TgGoods>) goodService
					.findGoodsByCategoryId(category.getId(), 1, 4, true)
					.getList());
		}
		return map;
	}

	private List<TgInformation> findHotActivity() {
		return informationService.findHotActivity();
	}

	private List<TgBrand> bulidHotTrend() {
		return brandService.bulidHotTrend();
	}

	@SuppressWarnings("unchecked")
	private Map<Long, List<TgBrand>> findHotTrendCategoryMap(
			List<TgCategory> categoryList) {
		Map<Long, List<TgBrand>> map = new HashMap<Long, List<TgBrand>>();
		for (Iterator iterator = categoryList.iterator(); iterator.hasNext();) {
			TgCategory category = (TgCategory) iterator.next();
			map.put(category.getId(), (List<TgBrand>) brandService
					.bulidBrandAd2(category.getId()));
		}
		return map;
	}
}
